import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import store from "../store/index.js"

const fn = (url, next) => {
  let rootRouter = store.state.userInfo.menus_url
  if (rootRouter.includes(url)) {
    next()
  } else {
    next("/")
  }
}

export const secondRouter = [
  {
    path: "menu",
    component: () => import("../page/menu/menu.vue"),
    meta: {
      title: "菜单管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/menu", next)
    }
  }, {
    path: "role",
    component: () => import("../page/role/role.vue"),
    meta: {
      title: "角色管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/role", next)
    }
  }, {
    path: "manage",
    component: () => import("../page/manage/manage.vue"),
    meta: {
      title: "管理员管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/manage", next)
    }
  }, {
    path: "cate",
    component: () => import("../page/cate/cate.vue"),
    meta: {
      title: "商品分类"
    },
    beforeEnter: (to, from, next) => {
      fn("/cate", next)
    }
  }, {
    path: "specs",
    component: () => import("../page/specs/specs.vue"),
    meta: {
      title: "商品规格"
    },
    beforeEnter: (to, from, next) => {
      fn("/specs", next)
    }
  }, {
    path: "goods",
    component: () => import("../page/goods/goods.vue"),
    meta: {
      title: "商品管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/goods", next)
    }
  }, {
    path: "member",
    component: () => import("../page/member/member.vue"),
    meta: {
      title: "会员管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/member", next)
    }
  }, {
    path: "banner",
    component: () => import("../page/banner/banner.vue"),
    meta: {
      title: "轮播图管理"
    },
    beforeEnter: (to, from, next) => {
      fn("/banner", next)
    }
  }, {
    path: "seckill",
    component: () => import("../page/seckill/seckill.vue"),
    meta: {
      title: "秒杀活动"
    },
    beforeEnter: (to, from, next) => {
      fn("/seckill", next)
    }
  }
]

const routes = [
  {
    path: "/login",
    component: () => import("../page/login/login.vue")
  },
  {
    path: "/",
    component: () => import("../page/index/index.vue"),
    children: [{
      path: "",
      component: () => import("../page/home/home.vue"),
      meta: {
        title: "首页"
      }
    },
    ...secondRouter]
  }]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    next()
    return;
  }
  if (store.state.userInfo.token) {
    next()
    return;
  }
  next("/login")

})
export default router
